package com.common.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.component.Pagination;
import com.common.manager.CparamManager;
import com.common.model.CparamModel;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/cparam")
public class CparamController extends BaseController{

	 private static String modelUrl="/management/cparam/";
	 
    @Autowired
	private CparamManager  cparamManager;

    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("cparamModel") CparamModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		List<CparamModel> dataList = cparamManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("objList", dataList);
		return modelView;
	}
	
	
    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("selectList")
	public ModelAndView selectList(HttpServletRequest request,@ModelAttribute("cparamModel") CparamModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"selectList");
		List<CparamModel> dataList = cparamManager.getObjectList(param);
		modelView.addObject("objList", dataList);
		String paramKey = request.getParameter("paramKey");
		if(StringUtils.isNotBlank(paramKey)){
			String[] valParam=paramKey.split("\\|");
			modelView.addObject("label",valParam[1]);
			modelView.addObject("pval",valParam[0]);
		}else{
			modelView.addObject("pval","''");
			modelView.addObject("label", "''");
		}
		
		return modelView;
	}
	
	/**
	 * 功能：初始化添加页面
	 * @param cparamModel
	 * @return
	 */
	@RequestMapping("initAdd")
	public ModelAndView initAdd(@ModelAttribute("cparamModel") CparamModel cparamModel) {
		ModelAndView modelView = new ModelAndView(modelUrl+"add");
		log.info("add is page");
		modelView.addObject("vo",cparamModel);
		return modelView;
	}
	
	/**
	 * 功能：初始化编辑页面
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/initEdit")
	public ModelAndView initEdit(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"update");
		IData dataMap = new DataMap();
		dataMap.put("intID", intID);
		CparamModel cparamModel = (CparamModel)cparamManager.getObjectInfo(dataMap);
		modelView.addObject("vo",cparamModel);
		return modelView;
	}
	
	
	/**
	 * 功能：执行插入操作
	 * @param cparamModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public Object add(HttpServletResponse response,@ModelAttribute("cparamModel") CparamModel cparamModel) {
		try{
			int opNum = cparamManager.insertObjectAndGetID(cparamModel);
			if(opNum>0){
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行更新操作
	 * @param cparamModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public Object update(HttpServletResponse response,@ModelAttribute("cparamModel") CparamModel cparamModel) {
		try{
			int opNum =cparamManager.updateObject(cparamModel);
			if(opNum>0){
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行单记录删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/{intID}/delete")
	public String delete(HttpServletResponse response,@PathVariable("intID") int intID) {
		try{
			IData dataMap = new DataMap();
			dataMap.put("intID", intID);
			int opNum =cparamManager.deleteObject(dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	/**
	 * 功能：执行批量删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteAll")
	public Object deleteAll(HttpServletResponse response,HttpServletRequest request) {
		try{
			String ids = request.getParameter("delids");
			IData dataMap = new DataMap();
			dataMap.put("ids", ids);
			int opNum =cparamManager.deleteObject("deleteAllObject",dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
}